package org.cocos2dx.javascript;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import com.reyun.tracking.sdk.Tracking;

import java.util.ArrayList;

public class reyunWrapper {
    private static final int REQUEST_PERMISSIONS_CODE = 22;
    private ArrayList<String> mNeedRequestPMSList=new ArrayList<>();
    private AppActivity _AppActivity;
    public static reyunWrapper instance;
    private reyunWrapper () {}
    public static reyunWrapper getInstance() {
        if (instance == null) {
            instance = new reyunWrapper();
        }
        return instance;
    }
    public void init(AppActivity app) {
            _AppActivity=app;
            checkAndRequestPermissions();
    }

    private void checkAndRequestPermissions() {
        /**
         * READ_PHONE_STATE 权限是必须权限，没有这个权限 SDK无法正常获得广告。
         */
        if (PackageManager.PERMISSION_GRANTED !=
                ActivityCompat.checkSelfPermission(_AppActivity, Manifest.permission.READ_PHONE_STATE)) {
            mNeedRequestPMSList.add(Manifest.permission.READ_PHONE_STATE);
        }


        /**
         * WRITE_EXTERNAL_STORAGE、 ACCESS_FINE_LOCATION 是两个可选权限；没有不影响 SDK
         获取广告；但是如果应用申请到该权限，会显著提升应用的广告收益。
         */
        if (PackageManager.PERMISSION_GRANTED !=
                ActivityCompat.checkSelfPermission(_AppActivity,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            mNeedRequestPMSList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if (PackageManager.PERMISSION_GRANTED !=
                ActivityCompat.checkSelfPermission(_AppActivity, Manifest.permission.ACCESS_FINE_LOCATION)) {
            mNeedRequestPMSList.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
        //
        if (0 == mNeedRequestPMSList.size()) {
            initTracking();
        } else {
            String[] temp = new String[mNeedRequestPMSList.size()];
            mNeedRequestPMSList.toArray(temp);
            ActivityCompat.requestPermissions(_AppActivity, temp,
                    REQUEST_PERMISSIONS_CODE);
        }
    }

    public void onRequestPermissionsResult(int requestCode) {
        switch (requestCode) {
            /**
             *处理 SDK 申请权限的结果。
             */
            case REQUEST_PERMISSIONS_CODE:
                if (hasNecessaryPMSGranted()) {
                    initTracking();
                }
                break;
            default:
                break;
        }
    }

    /**
     * 判断应用是否已经获得 SDK运行必须的 READ_PHONE_STATE、WRITE_EXTERNAL_STORAGE 两
     个权限。
     *
     * @return
     */
    private boolean hasNecessaryPMSGranted() {
        if (PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(AppActivity.getContext(), Manifest.permission.READ_PHONE_STATE)) {
            return true;
        }
        return false;
    }

    public void initTracking(){
        Tracking.initWithKeyAndChannelId(_AppActivity.getApplication(),Config.ry_appId,Config.ry_channel);
        Tracking.setDebugMode(Config.isSandBox);
    }
}
